﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Badhshala.Controllers
{
    public class BaseController : Controller
    {
        public long FacebookId
        {
            get
            {
                return long.Parse(User.Identity.Name);
            }
        }

        public string AccessToken
        {
            get
            {
                if (Session["AccessToken"] == null)
                {
                    return null;
                }
                else
                {
                    return Session["AccessToken"].ToString();
                }
            }
            set
            {
                Session["AccessToken"] = value;
            }
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            Exception ex = filterContext.Exception;
            StackTrace st = new StackTrace(ex, true);
            StackFrame sf = st.GetFrame(0);

            LogEntry log = new LogEntry();
            log.Title = ex.Message;
            log.Message = ex.StackTrace;

            if (sf != null)
            {
                log.Message += " <br /> Function" + sf.GetFileName();
                log.Message += " <br /> Method" + sf.GetMethod();
                log.Message += " <br /> LineNumber" + sf.GetFileLineNumber();
            }


            log.Message += " <br /> IPAddress:" + HttpContext.Request.UserHostAddress;
            log.Message += " <br /> Browser:" + HttpContext.Request.Browser.Browser;
            log.Message += " <br /> Operating System:" + HttpContext.Request.UserAgent;

            Logger.Write(log);

            filterContext.ExceptionHandled = true;
            filterContext.Result = this.RedirectToAction("GeneralErrorPage", "Error");
        }
    }
}
